<?php


namespace app\controller;


use app\model\goodsModel;
use think\facade\Request;

class goods
{
private $model;

    public function __construct()
    {
        $this->model = new goodsModel();
}

    public function getList()
    {
        $param=Request::get();
        file_put_contents('1.txt', var_export(11111, 1));
        $map = [];
        if (isset($param['dictionary_id'])&&$param['dictionary_id']){
            $map[] = ['dictionary_id', '=', $param['dictionary_id']];
        }
        if (isset($param['category_id'])&&$param['category_id']){
            $map[] = ['category_id', '=', $param['category_id']];
        }
        $data=$this->model->with(['dictionary','category'])->where($map)->paginate([
            'query' => $param,
            'var_page' => 'current_page',
            'list_rows' => $param['pagesize'],
        ])->toArray();
        if ($data) $meta=['status'=>200];
        else $meta=['status'=>400];
        return json(['data'=>$data,'meta'=>$meta]);
}
    public function add()
    {
        $data = Request::post();
        $this->model->addData($data);
        return json(['meta'=>['msg'=>'新增成功','status'=>200]]);
    }
    public function edit($id)
    {
        if (Request::isPut()) {
            // 获取POST数据
            $data = Request::put();
            // 入库

            $this->model->saveData($data);
            return json(['meta'=>['msg'=>'编辑成功','status'=>200]]);
        } else {
            // 如果没有get到id, 则跳转回列表页
//            $id = Request::get('id');
            // 当前设备信息
            $data = $this->model->with(['dictionary','category'])->find($id)->toArray();
            file_put_contents('1.txt', var_export($data, 1));
            return json(['data'=>$data,'meta'=>['msg'=>'编辑成功','status'=>200]]);
        }}
}